package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.domain.CheckItem;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.service.CheckItemService;

import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/checkItem")
public class CheckItemController {

    @Reference(check = false)
    private CheckItemService checkItemService;

    /**
     * 新增检查项
     *
     * @param checkItem
     * @return
     */
    @RequestMapping("/add")
    public Result insertCheckItem(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.insertCheckItem(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //提示错误信息
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        //提示成功消息
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }


    /**
     * 查询单项检查项
     *
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id) {

        CheckItem checkItem = checkItemService.findCheckItemById(id);
        if (checkItem == null) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        } else {
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        }
    }

    /**
     * 分页查询 条件查询
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findByPage")
    public PageResult findCheckItemByPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkItemService.findCheckItemByPage(queryPageBean);
        System.out.println(pageResult);
        return pageResult;
    }

    /**
     * 删除检查项
     *
     * @param id
     * @return
     */
    //权限校验
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/deleteCheckItem")
    public Result deleteCheckItem(Integer id) {
        try {
            checkItemService.deleteCheckItem(id);
        } catch (Exception e) {
            e.printStackTrace();
            //删除失败
            return new Result(false, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        //删除成功
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 修改检查项
     *
     * @return
     */
    @RequestMapping("/updateCheckItem")
    public Result updateCheckItem(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.updateCheckItem(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //编辑 修改失败
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        //编辑成功
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询所有检查项
     *
     * @return
     */
    @RequestMapping("/findAllCheckItems")
    public Result findAllCheckItems() {
        List<CheckItem> allCheckItems = checkItemService.findAllCheckItems();
        if (allCheckItems == null) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        } else {
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, allCheckItems);
        }

    }

}
